package com.gsafety.wellsfort.service.organization.impl;

import com.gsafety.wellsfort.dao.organization.WxCustomerDAO;
import com.gsafety.wellsfort.domain.model.organization.WxCustomer;
import com.gsafety.wellsfort.domain.vo.WeChatLoginVO;
import com.gsafety.wellsfort.service.organization.IWeChatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class WeChatServiceImpl implements IWeChatService {

    @Autowired
    private WxCustomerDAO wxCustomerDAO;

    @Override
    public int add(WeChatLoginVO weChatLoginVO) {
        return wxCustomerDAO.insertSelective(convertWeChatVO2Do(weChatLoginVO));
    }

    @Override
    public WxCustomer get(Integer id) {
        return wxCustomerDAO.selectByPrimaryKey(id);
    }

    @Override
    public WxCustomer getByOpenId(String openId) {
        return wxCustomerDAO.selectByOpenId(openId);
    }

    @Override
    public int update(WxCustomer wxCustomer) {
        return wxCustomerDAO.updateByPrimaryKeySelective(wxCustomer);
    }

    @Override
    public List<WxCustomer> selectByLoginId(Integer loginId) {
        return  wxCustomerDAO.selectByLoginId(loginId);
    }

    @Override
    public int delete(Integer id) {
        return wxCustomerDAO.deleteByPrimaryKey(id);
    }

    /**
     * @param weChatLoginVO
     * @return
     */
    private WxCustomer convertWeChatVO2Do(WeChatLoginVO weChatLoginVO) {
        WxCustomer wxCustomer = new WxCustomer();
        Date date = new Date();
        wxCustomer.setCreated(date);
        wxCustomer.setModified(date);
        BeanUtils.copyProperties(weChatLoginVO, wxCustomer);
        return wxCustomer;
    }
}
